/**
 * Copyright 2023 Inc.
 **/
package com.myz.springclond2021.listener;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.listener.impl.PropertiesListener;
import com.myz.springclond2021.config.DynamicThreadPoolConfig;
import com.myz.springclond2021.config.DynamicThreadPoolProperties;
import com.myz.springclond2021.utils.MapToPropertiesConverter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.yaml.snakeyaml.Yaml;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * @author maoyz0621 on 2023/10/7
 * @version v1.0
 */
@Configuration
@RefreshScope
@Slf4j
public class ConfigLister implements InitializingBean {

    @Resource
    private NacosConfigManager nacosConfigManager;


    @Resource
    private DynamicThreadPoolConfig dynamicThreadPoolConfig;

    @Autowired
    private NacosConfigProperties nacosConfigProperties;

    private static final String data_id = "threadPool.yaml";

    private static final String threadPool1 = "threadPool1.properties";

    @Override
    public void afterPropertiesSet() throws Exception {
        ////////////////////////////////////////////////// 方法1 //////////////////////////////////////////////////////////
        try {
            this.nacosConfigManager.getConfigService().addListener(data_id, nacosConfigProperties.getGroup(), new Listener() {

                @Override
                public Executor getExecutor() {
                    return null;
                }

                @SneakyThrows
                @Override
                public void receiveConfigInfo(String configInfo) {
                    /**
                     * thread:
                     *     enabled: true
                     *     corePoolSize: 39
                     *     maxPoolSize: 106
                     */
                    log.info("=======receiveConfigInfo=\n{}\n========", configInfo);
                    if (StringUtils.isEmpty(configInfo)) {
                        return;
                    }
                    Map<String, Object> bootMap = new Yaml().load(configInfo);
                    Properties properties = MapToPropertiesConverter.convert(bootMap);
                    resetDynamicThreadPoolConfig(properties);
                }
            });
        } catch (Exception e) {

        }


        ////////////////////////////////////////////////// 方法2 //////////////////////////////////////////////////////////
        try {
            this.nacosConfigManager.getConfigService().addListener(threadPool1, nacosConfigProperties.getGroup(), new PropertiesListener() {
                @Override
                public void innerReceive(Properties properties) {
                    log.info("========innerReceive={}=======", properties);
                    resetDynamicThreadPoolConfig(properties);
                }
            });
        } catch (NacosException e) {
        }
    }

    private void resetDynamicThreadPoolConfig(Properties properties) {
        DynamicThreadPoolProperties threadPoolProperties = new DynamicThreadPoolProperties();
        threadPoolProperties.setCorePoolSize(Integer.parseInt(properties.getProperty("thread.corePoolSize")));
        threadPoolProperties.setMaxPoolSize(Integer.parseInt(properties.getProperty("thread.maxPoolSize")));
        dynamicThreadPoolConfig.resetThreadPool(threadPoolProperties);
    }


}